<?php
namespace App\Services;
use App\Models\FeedBackModel;
use App\Models\MemberModel;

class QuestionService{
	/**
	 * 获取反馈信息
	 * @param string $question
	 * @return object $info
	 */
	public function getQuestionInfoByName($question){
		$info = FeedBackModel::where('content',$question)->first();
		return $info;
	}

	/**
	 * 添加反馈信息
	 * @param array $data
	 * @return bool $res
	 */
	public function insertQuestion($data){
		$res = FeedBackModel::insert($data);
		return $res;
	}

	/**
	 * 获取反馈信息列表
	 * @param int $pagesize
	 * @param string $keywords
	 * @return obj $qlist
	 */
	public function getQuestionList($pagesize){
		
		$qlist = FeedBackModel::orderBy('id','desc')
				 ->where('isshow',1)
				 ->select('id','member_id','content','add_date')
				 ->paginate($pagesize);
		foreach($qlist as $itm=>$lst){
			$minfo = MemberModel::where('id',$lst->member_id)->select('member_name','telephone')->first();
			if(!empty($minfo)){
				$qlist[$itm]->member_name = $minfo->member_name;
				$qlist[$itm]->telephone = $minfo->telephone;
			}
		}

		return $qlist;
	}

	public function searchQuestionList($pagesize,$keywords=''){
		$pattern = "/^1[34578]\d{9}$/"; 
		preg_match($pattern, $keywords, $match);
		if(empty($match)){
			$memberinfo = MemberModel::where('member_name',$keywords)->select('id','member_name','telephone')->first();
		}else{
			$memberinfo = MemberModel::where('telephone',$keywords)->select('id','member_name','telephone')->first();
		}

		if(!empty($memberinfo)){
			$qlist = FeedBackModel::orderBy('id','desc')
					 ->where('isshow',1)
					 ->where('member_id',$memberinfo->id)
					 ->select('id','content','add_date')
					 ->paginate($pagesize);

			foreach($qlist as $itm=>$lst){
				$qlist[$itm]->member_name = $memberinfo->member_name;
				$qlist[$itm]->telephone = $memberinfo->telephone;
			}

			return $qlist;
		}
	}

	public function delQuestion($id,$data){

		$res = FeedBackModel::where('id',$id)->update($data);
		return $res;
	}
}